package net.huashitong.utils;

import com.sirdc.modules.sys.service.SysTableService;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class UtilsTools {

    public final static String msgapiKey = "b3232c91e3336d52ae80b73bb0a1cb912";     //关闭
//    public final static String msgapiKey = "b3232c91e3336d52ae80b73bb0a1cb91";

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

    //获取当当前年月日
    public static String getCurrentDate() {

        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        String currentDate = sf.format(date);

        return currentDate;
    }

    //获取当前时间
    public static String getCurrentTime() {
		/*Calendar cal = Calendar.getInstance();
		int hour = cal.get(Calendar.HOUR_OF_DAY);
		int minute = cal.get(Calendar.MINUTE);
		int second = cal.get(Calendar.SECOND);*/
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("HH:mm:ss");
        String time = sf.format(date);
        return time;
    }

    //获取年份
    public static String getCurrenYear() {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy");
        String currentYear = sf.format(date);
        return currentYear;
    }

    //获取当前月份
    public static String getCurrentMonth() {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMM");
        String currentMonth = sf.format(date);
        return currentMonth;
    }

    //获取当前日期时间
    public static String getCurrentDateTime() {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        String currnetDateTime = sf.format(date);
        return currnetDateTime;
    }

    //获取当前日期时间（yyyy.MM.dd）
    public static String getCurrentDateTime1() {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy.MM.dd");
        String currnetDateTime = sf.format(date);
        return currnetDateTime;
    }

    //获取当前日期时间（yyyy-MM-dd）
    public static String getCurrentDateTime2() {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        String currnetDateTime = sf.format(date);
        return currnetDateTime;
    }

    /**
     * 获取过去一月的时间
     *
     * @return
     */
    public static String getLastMonthDateTime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH, -1);
        Date m = c.getTime();
        String mon = format.format(m);
        return mon;
    }

    /**
     * 字符串转日期 YYYYMMDD 格式
     *
     * @param StringDate
     * @return date
     */
    public static Date StringToDate(String StringDate) throws ParseException {
        // = new Date();
        Date date = sdf.parse(StringDate);
        return date;
    }

    /**
     * 日期转字符串 YYYMMDD
     *
     * @param date
     * @return
     */
    public static String DateToString(Date date) {

        String Stringdate = sdf.format(date);
        return Stringdate;
    }

    /**
     * 日期加天数的日期（过滤周末）
     *
     * @param num
     * @param StringDate
     * @return
     * @throws ParseException
     */
    public static String plusDay(int num, String StringDate) throws ParseException {
        Date date = sdf.parse(StringDate);
        System.out.println("现在日期为：" + date);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        //过滤周末
        for (int i = 1; i <= num; i++) {
            int week = cal.get(Calendar.DAY_OF_WEEK) - 1;
            while (week == 0 || week == 6){
                cal.add(Calendar.DATE, 1);
                week = cal.get(Calendar.DAY_OF_WEEK) - 1;
            }
            cal.add(Calendar.DATE, 1);
        }

        date = cal.getTime();
        String currentDate = sdf.format(date);
        System.out.println("改变后的日期为：" + currentDate);
        return currentDate;
    }

    /**
     * 数组去重
     * @param arr
     * @return
     */
    public static String[] removalRepeatArr(String[] arr){

        List<String> list = new ArrayList();
        for(int i=0;i<arr.length;i++){
            if(!list.contains(arr[i])){
                list.add(arr[i]);
            }
        }
        String[] result = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            result[i]= list.get(i);
        }
        return result;
    }

    /**
     * 获取客户端Ip
     * @param request
     * @return
     */
    public static String getClientIp(HttpServletRequest request) {
        String ipAddress = request.getHeader("x-forwarded-for");
        if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
            if(ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")){
                //根据网卡取本机配置的IP
                InetAddress inet=null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                ipAddress= inet.getHostAddress();
            }
        }
        //对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        if(ipAddress!=null && ipAddress.length()>15){ //"***.***.***.***".length() = 15
            if(ipAddress.indexOf(",")>0){
                ipAddress = ipAddress.substring(0,ipAddress.indexOf(","));
            }
        }
        return ipAddress;
    }
}
